package com.eurasia.assignment.service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.eurasia.assignment.domain.Assignment;
import com.eurasia.assignment.infrastructure.entity.AssignmentEntity;
import com.eurasia.assignment.infrastructure.repository.AssignmentRepository;
import com.eurasia.assignment.controller.dto.AssignmentSearchRequest;

@Service
public class AssignmentService {
    private final AssignmentRepository assignmentRepository;

    public AssignmentService(AssignmentRepository assignmentRepository) {
        this.assignmentRepository = assignmentRepository;
    }

    public Optional<Assignment> getAssignment(Long id) {
        return assignmentRepository.findById(id)
                .map(AssignmentEntity::toDomain);
    }

    public List<Assignment> findAll() {
        return assignmentRepository.findAll().stream()
                .map(AssignmentEntity::toDomain)
                .toList();
    }

    public Assignment createAssignment(Assignment assignment) {
        AssignmentEntity entity = AssignmentEntity.fromDomain(assignment);
        return assignmentRepository.save(entity).toDomain();
    }
    
    public boolean deleteAssignment(Long id) {
        if (assignmentRepository.existsById(id)) {
            assignmentRepository.deleteById(id);
            return true;
        }
        return false;
    }

    public List<Assignment> searchAssignments(AssignmentSearchRequest searchRequest) {
        return assignmentRepository.findAll().stream()
                .map(AssignmentEntity::toDomain)
                .filter(assignment -> matchesSearchCriteria(assignment, searchRequest))
                .collect(Collectors.toList());
    }
    
    public Page<Assignment> searchAssignmentsWithPagination(AssignmentSearchRequest searchRequest, int page, int size) {
        // 获取所有匹配的作业
        List<Assignment> filteredAssignments = assignmentRepository.findAll().stream()
                .map(AssignmentEntity::toDomain)
                .filter(assignment -> matchesSearchCriteria(assignment, searchRequest))
                .collect(Collectors.toList());
        
        // 手动分页
        Pageable pageable = PageRequest.of(page, size);
        int start = (int) pageable.getOffset();
        int end = Math.min((start + pageable.getPageSize()), filteredAssignments.size());
        
        // 考虑边界情况
        if (start > filteredAssignments.size()) {
            return new PageImpl<>(List.of(), pageable, filteredAssignments.size());
        }
        
        List<Assignment> pageContent = filteredAssignments.subList(start, end);
        
        // 创建分页结果
        return new PageImpl<>(pageContent, pageable, filteredAssignments.size());
    }

    private boolean matchesSearchCriteria(Assignment assignment, AssignmentSearchRequest searchRequest) {
        if (searchRequest.getName() != null && !searchRequest.getName().isEmpty()) {
            if (!assignment.getName().toLowerCase().contains(searchRequest.getName().toLowerCase())) {
                return false;
            }
        }
        
        if (searchRequest.getCourse() != null && !searchRequest.getCourse().isEmpty()) {
            if (!assignment.getCourse().toLowerCase().contains(searchRequest.getCourse().toLowerCase())) {
                return false;
            }
        }
        
        if (searchRequest.getType() != null && !searchRequest.getType().isEmpty()) {
            if (!assignment.getType().equals(searchRequest.getType())) {
                return false;
            }
        }
        
        if (searchRequest.getStatus() != null && !searchRequest.getStatus().isEmpty()) {
            if (!assignment.getStatus().equals(searchRequest.getStatus())) {
                return false;
            }
        }
        
        return true;
    }
} 